import { useRequest } from 'umi';
import {
  queryPlanModeListAPI,
  queryPlanModeInfoAPI,
  addPlanModeAPI,
  deletePlanModeAPI,
  editPlanModeAPI,
} from './services';
import { createModel } from 'hox';
import { useState } from 'react';
import { PlanListReturnType } from './API';
interface HoxModelProps {
  planModeInfo: PlanListReturnType;
}
function useHoxModel() {
  const [state, setState] = useState<HoxModelProps>({
    planModeInfo: {},
  });
  const dispatch = (payload: Partial<HoxModelProps>) => {
    setState((pre) => ({ ...pre, ...payload }));
  };

  //获取卡片接口
  const { run: getPlanModeList, data: planModelList } = useRequest(queryPlanModeListAPI, {
    manual: true,
  });
  //添加计划模板接口
  const { run: addPlanMode } = useRequest(addPlanModeAPI, {
    manual: true,
    formatResult: (res) => res,
  });
  //删除计划模板接口
  const { run: deletePlanMode } = useRequest(deletePlanModeAPI, {
    manual: true,
    formatResult: (res) => res,
  });
  //修改计划模板接口
  const { run: editPlanMode } = useRequest(editPlanModeAPI, {
    manual: true,
    formatResult: (res) => res,
  });
  //获取计划模板详情接口
  const { run: getPlanModeInfo, loading: planModeInfoLoading } = useRequest(queryPlanModeInfoAPI, {
    manual: true,
    onSuccess: (res, params) => {
      if (res?.result === 0) {
        dispatch({ planModeInfo: res.data });
      }
    },
    formatResult: (res) => res,
  });
  return {
    ...state,
    dispatch,
    deletePlanMode,
    addPlanMode,
    editPlanMode,
    getPlanModeList,
    getPlanModeInfo,
    planModelList,
    planModeInfoLoading,
  };
}
export default createModel(useHoxModel);
